import {Injectable} from '@angular/core';
import {Subscription, BehaviorSubject} from "rxjs";

import {AuthService} from "@shared/service/auth/auth.service";
import {HttpService} from "@core/http/http.service";
import {ToolsService} from "@shared/service/tools/tools.service";
import * as API from "@api/index";
import {FeedRespVO, UserAlbumFileVO, UserEventsVO, UserInfoVO, UserServiceVO} from "@interface/index";
import {Response} from "@angular/http";
// import {
//   FeedRespVO, UserAlbumFileVO, UserEventsVO, UserInfoVO,
//   UserServiceVO
// } from "@interface/interface/interface.model";


@Injectable()
export class HomepageService {
  subscription: Subscription = new Subscription();
  public _feeds: BehaviorSubject<FeedRespVO[]> = new BehaviorSubject<FeedRespVO[]>([]);
  public _gallery: BehaviorSubject<UserAlbumFileVO[]> = new BehaviorSubject<UserAlbumFileVO[]>([]);
  public _videoThumbnail: BehaviorSubject<UserAlbumFileVO[]> = new BehaviorSubject<UserAlbumFileVO[]>([]);
  public _service: BehaviorSubject<UserServiceVO[]> = new BehaviorSubject<UserServiceVO[]>([]);
  public _eventList: BehaviorSubject<UserEventsVO[]> = new BehaviorSubject<UserEventsVO[]>([]);
  public dataStore = {
    feeds: [] = [],
    gallery: [] = [],
    videoThumbnail: [] = []
  };
  public homepageStore = {
    dealService: [] = [],
    eventList: [] = [],

  };

  constructor(public user: AuthService,
              public httpService: HttpService,
              public toolsService: ToolsService
  ) {

  }

   // 查询用户个人信息
  getPeronInfo(obj) {
    const url = API.PERSON_INFO + '?userId=' + obj;
    return this.httpService.get(url).map((res) => res.json());
  }

   // 我的粉丝
  getMyfans(obj) {
    const url = API.PERSON_MY_FANS + '?userId=' + obj + '&rows=' + 20;
    return this.httpService.get(url).map((res) => res.json());
  }

   // 我的粉丝
  getFans(obj) {
    const url = API.PERSON_MY_FANS + '?userId=' + obj + '&rows=' + 8;
    return this.httpService.get(url).map((res) => res.json());
  }

   // 我关注的人
  getMycare(obj) {
    const url = API.PERSON_MY_CARE + '?userId=' + obj + '&rows=' + 20;
    return this.httpService.get(url).map((res) => res.json());
  }

   // 我关注的人
  getFollows(obj) {
    const url = API.PERSON_MY_CARE + '?userId=' + obj + '&rows=' + 8;
    return this.httpService.get(url).map((res) => res.json());
  }

   // 服务
  getService(obj) {
    const url = API.PERSON_SERVICE + '?userId=' + obj;
    return this.httpService.get(url).map((res) => res.json());
  }

  get getservice() {
    return this._service.asObservable();
  }

   // 获取服务信息
  getServiceInfo(obj) {
    this.getService(obj).subscribe(res => {
      if (res.result === '0') {
        this.homepageStore.dealService = res.data;
        this._service.next(this.homepageStore.dealService);
      }
    });
  }

   // 用户帖子
  get feeds() {
    return this._feeds.asObservable();
  }

  getUserFeed(userId: UserInfoVO, page: number = 1, rows: number = 2) {
    const url = API.GET_USER_FEED_URL + `?userId=${userId}&page=${page}&rows=${rows}`;
    this.httpService.get(url)
      .map(res => res.json())
      .subscribe((res) => {
        this.dataStore.feeds = [...res.data.list];
        this._feeds.next(this.dataStore.feeds);
      });
  }

   // 新增关注
  addCareFans(obj) {
    const obx = {followUserId: obj};
    const url = API.PERSON_ADD_CARE_FANS;
    return this.httpService.postUrlencode(url, obx).map((res) => res.json());
  }

   // 新增关注
  getAddCareFans(itemUserid, event, hasCare, careEach, i) {
    this.addCareFans(itemUserid).subscribe(res => {
      const obj = res;
      if (obj.result === '0') {
        if (obj.data.isFollowedMe === 0) { // 是否关注了我
          event.target.src = hasCare;
          event.target.className = 'clicked';
        } else {
          event.target.src = careEach;
        }
      }
    });
  }

   // 新增关注
  getAddCares(itemUserid, event, hasCare, careEach, i) {
    this.addCareFans(itemUserid).subscribe(res => {
      const obj = res;
      if (obj.result === '0') {
        event.target.className = 'clicked';
        if (obj.data.isFollowedMe === 0) { // 是否关注了我
          event.target.src = hasCare;
        } else {
          event.target.src = careEach;
        }
      }
    });
  }

   // 删除关注
  DeleteCareFans(obj) {
    const url = API.PERSON_DELETE_CARE_FANS + '?followUserId=' + obj;
    return this.httpService.delete(url).map((res) => res.json());
  }

  get getevent() {
    return this._eventList.asObservable();
  }

   // 获取赛事赛事列表
  events(obj) {
    this.getMatchs(obj).subscribe(res => {
      if (res.result === '0' && res.data.list.length > 0) {
        this.homepageStore.eventList = [...res.data.list];
      }
      this._eventList.next(this.homepageStore.eventList);
    });
  }

   // 赛事
  getMatchs(obj) {
    let url;
    url = API.PERSON_EVENTS + '?userId=' + obj + '&statusList=3,4,5,6,7,8&rows=6';
    return this.httpService.get(url).map((res) => res.json());
  }

  get gallery() {
    return this._gallery.asObservable();
  }

  get videoThumbnail() {
    return this._videoThumbnail.asObservable();
  }

  getUserAlbum(userId: UserInfoVO, resType: number = 2, purType: number = 1, page = 1, rows = 10) {
    const url = API.PERSON_PICTURE_VIDEO + `?userId=${userId}&resType=${resType}&purType=${purType}&page=${page}&rows=${rows}`;
    this.httpService.get(url)
      .map(res => res.json())
      .subscribe((res) => {
        if (res && res.result === '0' && res.data && res.data.list) {
          const data = res.data.list;
          if (resType === 2) {
            this.dataStore.gallery = [...data];
            this._gallery.next(this.dataStore.gallery);
          } else if (resType === 3) {
            this.dataStore.videoThumbnail = [...data];
            this._videoThumbnail.next(this.dataStore.videoThumbnail);
          }
        }
      });
  }

  /**
   * @description 获取我关注的人的动态信息（帖子）
   * @author ouyqx-691
   * @date 2018/2/5
   * @param params 请求参数
   */
  getDynamicInfomationOfMyFollowingPeople(params) {
    const url = API.FEED_GUEST_URL +  '?' + this.toolsService.param(params);
    return this.httpService.get(url).map((res: Response) => res.json());
  }
}










